package chapter5.homework.heima.Q2ReceiveAndFeedback.teachers;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Project ：learn_java
 * @File ：Server.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/14 09:07
 * @Description ：For study.
 */

public class Server {
    public static void main(String[] args) throws IOException {

        // 创建对象并连接10000端口
        ServerSocket ss = new ServerSocket(10000);

        // 等待客户端连接
        Socket socket = ss.accept();

        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        int b;
        while ((b = isr.read()) != - 1) {
            System.out.print((char) b);
        }

        // feedback data
        String str = "How happy you are?";
        OutputStream os = socket.getOutputStream();
        os.write(str.getBytes());

        // release resources
        socket.close();
        ss.close();
    }
}
